package com.rx.school.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rx.core.base.RxBaseConstant;
import com.rx.core.base.RxBaseData;
import com.rx.core.data.UrlAnalyse;
import com.rx.core.dbase.RxMarkDbMust;
import com.rx.core.enm.HANDLER_TYPE;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.enm.RX_STATUS;
import com.rx.core.enm.RX_WEB_STATUS;
import com.rx.core.handler.CommonHandler;
import com.rx.core.iface.TreeNode;
import com.rx.core.service.RxBaseAbstractService;
import com.rx.core.service.StandardService;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxUtil;
import com.rx.school.bean.SearchUrlJson;
import com.rx.school.dao.SchArticleInfoMapper;
import com.rx.school.service.proxy.OutProxyService;
import com.rx.school.vo.*;
import com.rx.school.vo.relation.*;
import com.rx.uua.vo.UuaOrgInfo;
import com.rx.uua.vo.UuaUserInfo;
import com.rx.uua.vo.common.DictVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import static com.rx.core.service.StandardService.RETURN_TREE;

@Slf4j
@Service
public class SchoolService extends RxBaseAbstractService {
    public static final String NEWEST = "1";
    @Autowired
    StandardService standardService;

    @Autowired
    OutProxyService outProxyService;

    @Autowired
    SchArticleInfoMapper schArticleInfoMapper;


    public Object schoolHome(SchoolHome schoolHome) throws IllegalAccessException, InstantiationException {
        RxUtil.CheckUtil.check(schoolHome.getOrgBid(), RESULT_CODE.EMPTY, "机构BID不能为空");

        SchCarouselInfo schCarouselInfo = (SchCarouselInfo) SchCarouselInfo.create(SchCarouselInfo.class, HANDLER_TYPE.SINGLE_LIST);
        schCarouselInfo.getHandlerController().onlyDefault();
        schCarouselInfo.setStatus(RX_STATUS.NORMAL.getCode());
        schCarouselInfo.setOrgBid(schoolHome.getOrgBid());

        Object object = standardService.searchList(schCarouselInfo);
        List list = RxUtil.result(object);
        schoolHome.setCarouselList(list);

        //默认所有学校都需要这些菜单。如果有学校不需要。则需要插入不需要的记录。
        SchMenuInfo schMenuInfo = (SchMenuInfo) SchMenuInfo.create(SchMenuInfo.class, HANDLER_TYPE.SINGLE_LIST);
        schMenuInfo.getHandlerController().onlyDefault();
        schMenuInfo.setStatus(RX_STATUS.FORBIDDEN.getCode());
        schMenuInfo.setOrgBid(schoolHome.getOrgBid());
        schMenuInfo.setReturnType(RETURN_TREE);

        object = standardService.searchList(schMenuInfo);
        List<SchMenuInfo> forbiddenList = RxUtil.result(object);

        schMenuInfo.setStatus(RX_STATUS.NORMAL.getCode());
        schMenuInfo.setOrgBid(RxBaseConstant.ORG_DEFAULT);
        object = standardService.searchList(schMenuInfo);
        List<SchMenuInfo> normalList = RxUtil.result(object);

        if (EmptyChecker.notEmpty(forbiddenList)) {
            Map<String, List<SchMenuInfo>> map = forbiddenList.stream().collect(Collectors.groupingBy(SchMenuInfo::getMenuCode));
            Iterator<SchMenuInfo> iterator = normalList.iterator();
            while (iterator.hasNext()) {
                SchMenuInfo item = iterator.next();
                if (EmptyChecker.notEmpty(map.get(item.getMenuCode()))) {
                    iterator.remove();
                }
            }
        }


        schoolHome.setPublicMenuList(normalList);


        DoubleSchPresidentInfo doubleSchPresidentInfo = (DoubleSchPresidentInfo) RxBaseData.create(
                DoubleSchPresidentInfo.class, SchColumnistInfo.class, HANDLER_TYPE.DOUBLE_LIST
        );
        doubleSchPresidentInfo.getHandlerController().onlyDefault();
        doubleSchPresidentInfo.setStatus(RX_STATUS.NORMAL.getCode());
        doubleSchPresidentInfo.setOrgBid(schoolHome.getOrgBid());

        SchColumnistInfo schColumnistInfo = new SchColumnistInfo();
        schColumnistInfo.setNewest(NEWEST);
        schColumnistInfo.setReturnType(SchColumnistInfo.ASS_USER_BID);
        doubleSchPresidentInfo.setRelation(schColumnistInfo);

        object = standardService.innerSearchList(doubleSchPresidentInfo);
        List<DoubleSchPresidentInfo> schPresidentInfoList = RxUtil.result(object);
        if (EmptyChecker.notEmpty(schPresidentInfoList)) {
            schoolHome.setPresidentInfoList(schPresidentInfoList);
            List<UuaUserInfo> uuaUserInfoList = new ArrayList<>();

            for (DoubleSchPresidentInfo personInfo : schPresidentInfoList) {
                UuaUserInfo uuaUserInfo = new UuaUserInfo();
                uuaUserInfo.setBid(personInfo.getUserBid());
                uuaUserInfoList.add(uuaUserInfo);
            }

            uuaUserInfoList = outProxyService.getUserList(uuaUserInfoList);
            if (EmptyChecker.notEmpty(uuaUserInfoList)) {

                for (DoubleSchPresidentInfo schPresidentInfo : schPresidentInfoList) {
                    for (UuaUserInfo uuaUserInfo : uuaUserInfoList) {
                        if (uuaUserInfo.getBid().equalsIgnoreCase(schPresidentInfo.getUserBid())) {
                            schPresidentInfo.setUserInfo(uuaUserInfo);
                        }
                    }
                }
            }

        }


        UuaOrgInfo uuaOrgInfo = new UuaOrgInfo();
        uuaOrgInfo.setStatus(RX_STATUS.NORMAL.getCode());
        uuaOrgInfo.setBid(schoolHome.getOrgBid());
        schoolHome.setOrgInfo(outProxyService.getDetail(uuaOrgInfo));

        return schoolHome;
    }

    public Object schoolHomeInfo(SchHomeInfo schoolHome) throws IllegalAccessException, InstantiationException {
        RxUtil.CheckUtil.check(schoolHome.getOrgBid(), RESULT_CODE.EMPTY, "机构BID不能为空");

        SchHomeInfo schHomeInfo = (SchHomeInfo) SchHomeInfo.create(SchHomeInfo.class, HANDLER_TYPE.SINGLE_LIST);
        schHomeInfo.getHandlerController().onlyDefault();
        schHomeInfo.setOrgBid(schoolHome.getOrgBid());
        schHomeInfo.setStatus(RX_STATUS.NORMAL.getCode());
        Object object = standardService.searchList(schHomeInfo);
        List<SchHomeInfo> schHomeInfoList = RxUtil.result(object);

        if (EmptyChecker.isEmpty(schHomeInfoList)) {
            return null;
        }

        List<DoubleSchHomeInfo> list = new ArrayList<>();
        for (SchHomeInfo homeInfo : schHomeInfoList) {
            DoubleSchHomeInfo doubleSchHomeInfo = new DoubleSchHomeInfo();
            RxUtil.copyProperties(homeInfo, doubleSchHomeInfo);
            list.add(doubleSchHomeInfo);
        }

        SchArticleInfo schArticleInfo = (SchArticleInfo) SchArticleInfo.create(SchArticleInfo.class, HANDLER_TYPE.SINGLE_LIST);
        schArticleInfo.getHandlerController().onlyDefault();
        schArticleInfo.setOrgBid(schoolHome.getOrgBid());
        schArticleInfo.setStatus(RX_STATUS.NORMAL.getCode());
        // 根据字段ID 进行倒叙排列查询
        RxUtil.SQL.desc(schArticleInfo, RxMarkDbMust.ID);

        object = standardService.searchList(schArticleInfo);
        List<SchArticleInfo> schArticleInfoList = RxUtil.result(object);

        if (EmptyChecker.notEmpty(schArticleInfoList)) {
            CommonHandler.showFields(schArticleInfo, Arrays.asList(RxUtil.StringUtil.lineToHump(RxMarkDbMust.CREATE_DATE),
                    RxUtil.StringUtil.lineToHump(RxMarkDbMust.CREATE_BY_ID),
                    RxUtil.StringUtil.lineToHump(RxMarkDbMust.UPDATE_BY_ID)
            ));

            Map<String, List<SchArticleInfo>> map = schArticleInfoList.stream().collect(Collectors.groupingBy(e -> e.getHomeTypeBid()));
            for (Map.Entry<String, List<SchArticleInfo>> stringListEntry : map.entrySet()) {
                for (DoubleSchHomeInfo doubleSchHomeInfo : list) {
                    if (doubleSchHomeInfo.getBid().equals(stringListEntry.getKey())) {
                        doubleSchHomeInfo.assignList(stringListEntry.getValue());
                    }
                }
            }
        }

        SchPersonInfo schPersonInfo = (SchPersonInfo) SchPersonInfo.create(SchPersonInfo.class, HANDLER_TYPE.SINGLE_LIST);
        schPersonInfo.getHandlerController().onlyDefault();
        schPersonInfo.setOrgBid(schoolHome.getOrgBid());
        schPersonInfo.setStatus(RX_STATUS.NORMAL.getCode());

        object = standardService.searchList(schPersonInfo);
        List<SchPersonInfo> schPersonInfoList = RxUtil.result(object);

        if (EmptyChecker.notEmpty(schPersonInfoList)) {

            List<UuaUserInfo> uuaUserInfoList = new ArrayList<>();
            List<DoubleSchPersonInfo> doubleSchPersonInfoList = new ArrayList<>();
            for (SchPersonInfo personInfo : schPersonInfoList) {
                UuaUserInfo uuaUserInfo = new UuaUserInfo();
                uuaUserInfo.setBid(personInfo.getUserBid());
                uuaUserInfoList.add(uuaUserInfo);
                doubleSchPersonInfoList.add(DoubleSchPersonInfo.create(personInfo));
            }
            uuaUserInfoList = outProxyService.getUserList(uuaUserInfoList);
            if (EmptyChecker.notEmpty(uuaUserInfoList)) {
                Map<String, List<UuaUserInfo>> map = uuaUserInfoList.stream().collect(Collectors.groupingBy(e -> e.getBid()));
                for (DoubleSchPersonInfo doubleSchPersonInfo : doubleSchPersonInfoList) {
                    doubleSchPersonInfo.setRelation(getUserFromMap(map, doubleSchPersonInfo.getUserBid()));
                }
            }

            Map<String, List<DoubleSchPersonInfo>> map = doubleSchPersonInfoList.stream().collect(Collectors.groupingBy(e -> e.getHomeTypeBid()));
            for (Map.Entry<String, List<DoubleSchPersonInfo>> stringListEntry : map.entrySet()) {
                for (DoubleSchHomeInfo doubleSchHomeInfo : list) {
                    if (doubleSchHomeInfo.getBid().equals(stringListEntry.getKey())) {
                        doubleSchHomeInfo.assignList(stringListEntry.getValue());
                    }
                }
            }
        }

        SchMoreArticleInfo schMoreArticleInfo = (SchMoreArticleInfo) SchMoreArticleInfo.create(SchMoreArticleInfo.class, HANDLER_TYPE.SINGLE_LIST);
        schMoreArticleInfo.getHandlerController().onlyDefault();
        schMoreArticleInfo.setOrgBid(schoolHome.getOrgBid());
        schMoreArticleInfo.setStatus(RX_STATUS.NORMAL.getCode());

        object = standardService.searchList(schMoreArticleInfo);
        List<SchMoreArticleInfo> schMoreArticleInfoList = RxUtil.result(object);
        if (EmptyChecker.notEmpty(schMoreArticleInfoList)) {
            Map<String, List<SchMoreArticleInfo>> map = schMoreArticleInfoList.stream().collect(Collectors.groupingBy(e -> e.getHomeTypeBid()));
            for (Map.Entry<String, List<SchMoreArticleInfo>> stringListEntry : map.entrySet()) {
                for (DoubleSchHomeInfo doubleSchHomeInfo : list) {
                    if (doubleSchHomeInfo.getBid().equals(stringListEntry.getKey())) {
                        doubleSchHomeInfo.assignList(stringListEntry.getValue());
                    }
                }
            }
        }

        return list.stream().sorted(Comparator.comparingInt(e -> e.getSort())).collect(Collectors.toList());
    }


    private int addUserBySetCheck(int setSize, List<UuaUserInfo> uuaUserInfoList, Set<String> userBidSet, String bid) {
        userBidSet.add(bid);
        if (setSize < userBidSet.size()) {
            setSize = userBidSet.size();
            UuaUserInfo uuaUserInfo = new UuaUserInfo();
            uuaUserInfo.setBid(bid);
            uuaUserInfoList.add(uuaUserInfo);
        }
        return setSize;
    }

    private UuaUserInfo getUserFromMap(Map<String, List<UuaUserInfo>> map, String bid) {
        List<UuaUserInfo> listUser = map.get(bid);
        if (EmptyChecker.notEmpty(listUser)) {
            listUser.get(0).fieldSetNull();
            return listUser.get(0).jsonClone();
        }
        return null;
    }


    public Object schoolArticle(SchArticleInfo param) throws IllegalAccessException, InstantiationException {

        RxUtil.CheckUtil.check(param.getOrgBid(), RESULT_CODE.MESSAGE, "学校编码为空");
        SchArticleInfo schArticleInfo = (SchArticleInfo) SchArticleInfo.create(SchArticleInfo.class, HANDLER_TYPE.DOUBLE_LEFT_PAGE);
        RxUtil.copyProperties(param, schArticleInfo);

        Map<String, Object> mapConditon = schArticleInfo.conditionMap();
        mapConditon.put("inStatus", Arrays.asList(RX_WEB_STATUS.NORMAL.getCode(), RX_WEB_STATUS.HOME.getCode()));

        RxUtil.SQL.desc(schArticleInfo, RxMarkDbMust.ID);
        schArticleInfo.getHandlerController().onlyDefault();

        Object object = standardService.searchPage(schArticleInfo);
        List<SchArticleInfo> schArticleInfoList = RxUtil.result(object);


        List<DoubleSchArticleInfo> list = new ArrayList<>();
        Set<String> userBidSet = new HashSet<>();
        int setSize = 0;
        List<UuaUserInfo> uuaUserInfoList = new ArrayList<>();
        if (EmptyChecker.notEmpty(schArticleInfoList)) {
            for (SchArticleInfo articleInfo : schArticleInfoList) {
                list.add(DoubleSchArticleInfo.create(articleInfo));
                setSize = addUserBySetCheck(setSize, uuaUserInfoList, userBidSet, articleInfo.getCreateById());
                setSize = addUserBySetCheck(setSize, uuaUserInfoList, userBidSet, articleInfo.getUpdateById());
            }
            uuaUserInfoList = outProxyService.getUserList(uuaUserInfoList);
            if (EmptyChecker.notEmpty(uuaUserInfoList)) {
                Map<String, List<UuaUserInfo>> map = uuaUserInfoList.stream().collect(Collectors.groupingBy(e -> e.getBid()));
                for (DoubleSchArticleInfo doubleSchArticleInfo : list) {
                    doubleSchArticleInfo.setCreateUser(getUserFromMap(map, doubleSchArticleInfo.getCreateById()));
                    doubleSchArticleInfo.setUpdateUser(getUserFromMap(map, doubleSchArticleInfo.getUpdateById()));
                }
            }
            ((IPage) schArticleInfo.getResultData()).setRecords(list);
        }

        // 前端临时显示隐藏字段
        CommonHandler.showFields(schArticleInfo, Arrays.asList(RxUtil.StringUtil.lineToHump(RxMarkDbMust.CREATE_DATE)));

        return object;
    }

    public Object schoolArticlePerson(List<String> bidList) throws IllegalAccessException, InstantiationException {

        bidList = bidList.stream().distinct().collect(Collectors.toList());
        List<UuaUserInfo> uuaUserInfoList = new ArrayList<>();
        for (String bid : bidList) {
            UuaUserInfo uuaUserInfo = new UuaUserInfo();
            uuaUserInfo.setBid(bid);
            uuaUserInfoList.add(uuaUserInfo);
        }
        uuaUserInfoList = outProxyService.getUserList(uuaUserInfoList);
        if (EmptyChecker.notEmpty(uuaUserInfoList)) {
            for (UuaUserInfo uuaUserInfo : uuaUserInfoList) {
                uuaUserInfo.appendFilter(Arrays.asList(RxUtil.StringUtil.lineToHump(UuaUserInfo.PHONE.toLowerCase()),
                        RxUtil.StringUtil.lineToHump(UuaUserInfo.EMAIL.toLowerCase()),
                        RxUtil.StringUtil.lineToHump(UuaUserInfo.ID_NO.toLowerCase()),
                        RxUtil.StringUtil.lineToHump(UuaUserInfo.PASSWORD.toLowerCase()),
                        RxUtil.StringUtil.lineToHump(UuaUserInfo.SALT.toLowerCase())));
            }
        }


        return uuaUserInfoList;
    }

    public Object schoolLeaderPerson(@RequestBody SchPresidentInfo param) throws IllegalAccessException, InstantiationException {
        RxUtil.CheckUtil.check(param.getOrgBid(), RESULT_CODE.MESSAGE, "学校编码为空");
        SchPresidentInfo schPresidentInfo = (SchPresidentInfo) SchPresidentInfo.create(SchPresidentInfo.class, HANDLER_TYPE.SINGLE_PAGE);
        RxUtil.copyProperties(param, schPresidentInfo);
        Map<String, Object> mapConditon = schPresidentInfo.conditionMap();
        mapConditon.put("inStatus", Arrays.asList(RX_WEB_STATUS.NORMAL.getCode(), RX_WEB_STATUS.HOME.getCode()));

        RxUtil.SQL.asc(schPresidentInfo, SchPresidentInfo.POSITION_TYPE);
        schPresidentInfo.getHandlerController().onlyDefault();

        Object object = standardService.searchPage(schPresidentInfo);
        List<SchPresidentInfo> schArticleInfoList = RxUtil.result(object);

        List<DoubleSchPresidentInfo> list = new ArrayList<>();
        Set<String> userBidSet = new HashSet<>();
        int setSize = 0;
        List<UuaUserInfo> uuaUserInfoList = new ArrayList<>();
        if (EmptyChecker.notEmpty(schArticleInfoList)) {
            for (SchPresidentInfo presidentInfo : schArticleInfoList) {
                list.add(DoubleSchPresidentInfo.create(presidentInfo));
                setSize = addUserBySetCheck(setSize, uuaUserInfoList, userBidSet, presidentInfo.getUserBid());
            }
            uuaUserInfoList = outProxyService.getUserList(uuaUserInfoList);
            if (EmptyChecker.notEmpty(uuaUserInfoList)) {
                Map<String, List<UuaUserInfo>> map = uuaUserInfoList.stream().collect(Collectors.groupingBy(e -> e.getBid()));
                for (DoubleSchPresidentInfo doubleSchPresidentInfo : list) {
                    doubleSchPresidentInfo.setUserInfo(getUserFromMap(map, doubleSchPresidentInfo.getUserBid()));
                }
            }
            ((IPage) schPresidentInfo.getResultData()).setRecords(list);
        }
        return object;
    }

    public Object schoolNormalPerson(@RequestBody SchPersonInfo param) throws IllegalAccessException, InstantiationException {
        RxUtil.CheckUtil.check(param.getOrgBid(), RESULT_CODE.MESSAGE, "学校编码为空");
        SchPersonInfo schPersonInfo = (SchPersonInfo) SchPersonInfo.create(SchPersonInfo.class, HANDLER_TYPE.SINGLE_PAGE);
        RxUtil.copyProperties(param, schPersonInfo);
        Map<String, Object> mapConditon = schPersonInfo.conditionMap();
        mapConditon.put("inStatus", Arrays.asList(RX_WEB_STATUS.NORMAL.getCode(), RX_WEB_STATUS.HOME.getCode()));

        RxUtil.SQL.asc(schPersonInfo, SchPersonInfo.POSITION_TYPE);
        schPersonInfo.getHandlerController().onlyDefault();

        Object object = standardService.searchPage(schPersonInfo);
        List<SchPersonInfo> schArticleInfoList = RxUtil.result(object);

        List<DoubleSchPersonInfo> list = new ArrayList<>();
        Set<String> userBidSet = new HashSet<>();
        int setSize = 0;
        List<UuaUserInfo> uuaUserInfoList = new ArrayList<>();
        if (EmptyChecker.notEmpty(schArticleInfoList)) {
            for (SchPersonInfo personInfo : schArticleInfoList) {
                list.add(DoubleSchPersonInfo.create(personInfo));
                setSize = addUserBySetCheck(setSize, uuaUserInfoList, userBidSet, personInfo.getUserBid());
            }
            uuaUserInfoList = outProxyService.getUserList(uuaUserInfoList);
            if (EmptyChecker.notEmpty(uuaUserInfoList)) {
                Map<String, List<UuaUserInfo>> map = uuaUserInfoList.stream().collect(Collectors.groupingBy(e -> e.getBid()));
                for (DoubleSchPersonInfo doubleSchPersonInfo : list) {
                    doubleSchPersonInfo.setUserInfo(getUserFromMap(map, doubleSchPersonInfo.getUserBid()));
                }
            }
            ((IPage) schPersonInfo.getResultData()).setRecords(list);
        }
        return object;
    }

    public Object schoolHistoryPerson(@RequestBody SchPresidentHistory param) throws IllegalAccessException, InstantiationException {
        RxUtil.CheckUtil.check(param.getOrgBid(), RESULT_CODE.MESSAGE, "学校编码为空");
        SchPresidentHistory schPersonInfo = (SchPresidentHistory) SchPresidentHistory.create(SchPresidentHistory.class, HANDLER_TYPE.SINGLE_PAGE);
        RxUtil.copyProperties(param, schPersonInfo);
        Map<String, Object> mapConditon = schPersonInfo.conditionMap();
        mapConditon.put("inStatus", Arrays.asList(RX_WEB_STATUS.NORMAL.getCode(), RX_WEB_STATUS.HOME.getCode()));

        RxUtil.SQL.asc(schPersonInfo, SchPresidentHistory.POSITION_TYPE);
        schPersonInfo.getHandlerController().onlyDefault();

        Object object = standardService.searchPage(schPersonInfo);
        List<SchPresidentHistory> schArticleInfoList = RxUtil.result(object);

        List<DoubleSchPresidentHistory> list = new ArrayList<>();
        Set<String> userBidSet = new HashSet<>();
        int setSize = 0;
        List<UuaUserInfo> uuaUserInfoList = new ArrayList<>();
        if (EmptyChecker.notEmpty(schArticleInfoList)) {
            for (SchPresidentHistory personInfo : schArticleInfoList) {
                list.add(DoubleSchPresidentHistory.create(personInfo));
                setSize = addUserBySetCheck(setSize, uuaUserInfoList, userBidSet, personInfo.getUserBid());
            }
            uuaUserInfoList = outProxyService.getUserList(uuaUserInfoList);
            if (EmptyChecker.notEmpty(uuaUserInfoList)) {
                Map<String, List<UuaUserInfo>> map = uuaUserInfoList.stream().collect(Collectors.groupingBy(e -> e.getBid()));
                for (DoubleSchPresidentHistory doubleSchPersonInfo : list) {
                    doubleSchPersonInfo.setUserInfo(getUserFromMap(map, doubleSchPersonInfo.getUserBid()));
                }
            }
            ((IPage) schPersonInfo.getResultData()).setRecords(list);
        }
        return object;
    }

    private boolean yesRemove(SchMenuInfo menuInfo, List<SchMenuInfo> forbiddenList) {
//        if(RxBaseConstant.ORG_ROOT.equalsIgnoreCase(menuInfo.getParentCode())){
//            return true;
//        }
        if (EmptyChecker.notEmpty(forbiddenList)) {
            for (SchMenuInfo schMenuInfo : forbiddenList) {
                if (menuInfo.getMenuCode().equals(schMenuInfo.getMenuCode())) {
                    return true;
                }
            }
        }
        return false;
    }

    public Object schoolPartyBuild(SchArticleInfo param) throws IllegalAccessException, InstantiationException {

        RxUtil.CheckUtil.check(param.getOrgBid(), RESULT_CODE.MESSAGE, "学校编码为空");
        RxUtil.CheckUtil.check(param.getHomeTypeBid(), RESULT_CODE.MESSAGE, "homeTypeBid为空");
        SchMenuInfo schMenuInfo = (SchMenuInfo) SchMenuInfo.create(SchMenuInfo.class, HANDLER_TYPE.SINGLE_LIST);
        schMenuInfo.setOrgBid(param.getHomeTypeBid());
        schMenuInfo.setStatus(RX_STATUS.NORMAL.getCode());
        param.setHomeTypeBid(null);

        schMenuInfo.getHandlerController().onlyDefault();

        Object object = standardService.searchList(schMenuInfo);
        List<SchMenuInfo> schMenuList = RxUtil.result(object);


        if (EmptyChecker.notEmpty(schMenuList)) {
            //默认所有学校都需要这些菜单。如果有学校不需要。则需要插入不需要的记录。
            schMenuInfo.setStatus(RX_STATUS.FORBIDDEN.getCode());
            schMenuInfo.setOrgBid(param.getOrgBid());

            object = standardService.searchList(schMenuInfo);
            List<SchMenuInfo> forbiddenList = RxUtil.result(object);

            List<DoubleSchMenuInfo> resultList = new ArrayList<>();
            List<String> typeList = new ArrayList<>();
            schMenuList = schMenuList.stream().sorted(Comparator.comparingLong(SchMenuInfo::getSort)).collect(Collectors.toList());

            for (SchMenuInfo menuInfo : schMenuList) {
                if (yesRemove(menuInfo, forbiddenList)) {
                    continue;
                }
                resultList.add(DoubleSchMenuInfo.create(menuInfo));
                if (EmptyChecker.notEmpty(menuInfo.getSearchUrlJson())) {
                    SearchUrlJson searchUrlJson = JSON.parseObject(menuInfo.getSearchUrlJson(), SearchUrlJson.class);
                    typeList.add(searchUrlJson.getParam().getType());
                }

            }
            if (EmptyChecker.notEmpty(typeList)) {
                List<SchArticleInfo> list = schArticleInfoMapper.searchListByTypeGroup(param, typeList);
                if (EmptyChecker.notEmpty(list)) {
                    for (Object o : list) {
                        CommonHandler.removeFilter(o, Arrays.asList(RxUtil.StringUtil.lineToHump(RxMarkDbMust.CREATE_DATE),
                                RxUtil.StringUtil.lineToHump(RxMarkDbMust.CREATE_BY_ID),
                                RxUtil.StringUtil.lineToHump(RxMarkDbMust.UPDATE_BY_ID)));
                    }
                    Map<String, List<SchArticleInfo>> map = list.stream().collect(Collectors.groupingBy(e -> e.getType()));
                    for (DoubleSchMenuInfo menuInfo : resultList) {
                        if (EmptyChecker.notEmpty(menuInfo.getSearchUrlJson())) {
                            SearchUrlJson searchUrlJson = JSON.parseObject(menuInfo.getSearchUrlJson(), SearchUrlJson.class);
                            List<SchArticleInfo> records = map.get(searchUrlJson.getParam().getType());
                            menuInfo.setRecords(records);
                        }

                    }
                }
            }


            return resultList;

        }


        return object;
    }

    public Object linkList(@RequestBody SchLinkInfo param) throws IllegalAccessException, InstantiationException {

        RxUtil.CheckUtil.check(param.getOrgBid(), RESULT_CODE.MESSAGE, "学校编码为空");
        SchLinkInfo schLinkInfo = (SchLinkInfo) SchLinkInfo.create(SchLinkInfo.class, HANDLER_TYPE.SINGLE_PAGE);
        RxUtil.copyProperties(param, schLinkInfo);
        schLinkInfo.setOrgBid(param.getOrgBid());
        schLinkInfo.setStatus(RX_WEB_STATUS.NORMAL.getCode());
        schLinkInfo.getHandlerController().onlyDefault();

        Object object = standardService.searchPage(schLinkInfo);
        CommonHandler.showFields(schLinkInfo, Arrays.asList(RxUtil.StringUtil.lineToHump(RxMarkDbMust.CREATE_DATE)
        ));
        return object;
    }

    private List<SchScoresInfo> getListFromDb(SubjectScoresInfo param) throws IllegalAccessException, InstantiationException{
        RxUtil.CheckUtil.check(param.getParentBid(), RESULT_CODE.MESSAGE, "学校编码为空");
        RxUtil.CheckUtil.check(param.getExamBid(), RESULT_CODE.MESSAGE, "考试编码为空");

        SchScoresInfo schScoresInfo = (SchScoresInfo) SchScoresInfo.create(SchScoresInfo.class, HANDLER_TYPE.SINGLE_LIST);
        RxUtil.copyProperties(param, schScoresInfo);
        schScoresInfo.setStatus(RX_STATUS.NORMAL.getCode());
        schScoresInfo.getHandlerController().onlyDefault();

        Object object = standardService.searchList(schScoresInfo);

        return RxUtil.result(object);
    }

    private SchFullScores getFullScores(String parentBid,String examBid) throws IllegalAccessException, InstantiationException{

        SchFullScores schFullScores = (SchFullScores)SchFullScores.create(SchFullScores.class,HANDLER_TYPE.SINGLE_LIST);
        schFullScores.setParentBid(parentBid);
        Map<String,Object> condition = new HashMap<>();
        condition.put("inExamBid",Arrays.asList(RxBaseConstant.BID_DEFAULT,examBid));
        schFullScores.setCondition(condition);

        Object object = standardService.searchList(schFullScores);

        List<SchFullScores> fullScores =  RxUtil.result(object);
        if(EmptyChecker.isEmpty(fullScores)){
           return SchFullScores.create();
        }

        if(fullScores.size() == 1){
            return fullScores.get(0);
        }

        for (SchFullScores fullScore : fullScores) {
            if(!RxBaseConstant.BID_DEFAULT.equalsIgnoreCase(fullScore.getExamBid())){
                return fullScore;
            }
        }
        return SchFullScores.create();
    }

    public Object subjectScoresList(@RequestBody SubjectScoresInfo param) throws IllegalAccessException, InstantiationException {

        List<SchScoresInfo> list = getListFromDb(param);
        if (EmptyChecker.isEmpty(list)) {
            return list;
        }


        List<SubjectScoresInfo> subjectScoresInfoList = new ArrayList<>();
        for (SchScoresInfo scoresInfo : list) {
            SubjectScoresInfo info = new SubjectScoresInfo();
            RxUtil.copyProperties(scoresInfo, info);
            Object scores = RxUtil.Reflection.getFieldValue(scoresInfo, param.getSubject());
            if (scores instanceof BigDecimal) {
                info.setSubjectScores((BigDecimal) scores);
            }

            subjectScoresInfoList.add(info);
        }

        return subjectScoresInfoList;
    }

    @Transactional
    public Object subjectScoresStatistics(@RequestBody SubjectScoresInfo param) throws IllegalAccessException, InstantiationException {

        // 统计。
        List<SchScoresInfo> list = getListFromDb(param);
        if (EmptyChecker.isEmpty(list)) {
            return Integer.valueOf(0);
        }

        SchExamInfo schExamInfo = (SchExamInfo)SchExamInfo.create(SchExamInfo.class,HANDLER_TYPE.SINGLE_DETAIL);
        schExamInfo.setBid(param.getExamBid());
        schExamInfo.getHandlerController().onlyDefault();
        schExamInfo = (SchExamInfo)standardService.detail(schExamInfo);
        if (EmptyChecker.isEmpty(schExamInfo)) {
            return Integer.valueOf(0);
        }


        SchFullScores fullScores = getFullScores(param.getParentBid(),param.getExamBid());



        SchScoresInfo total = new SchScoresInfo();
        SchExamInfo pass = new SchExamInfo();
        total.initScores();
        List<RxBaseData> updateBatch = new ArrayList<>();
        schExamInfo.zeroPassCount();
        for (SchScoresInfo schScoresInfo : list) {
            total.calcTotal(schScoresInfo);
            if(schScoresInfo.addAllScores()){
                updateBatch.add(schScoresInfo);
            }
            schExamInfo.calcPassCount(schScoresInfo,fullScores);
        }

        Integer count = schExamInfo.getCount();
        if( null == count || count == 0 ){
            count = list.size();
            schExamInfo.setCount(count);
        }else {
            if(list.size() > count){
                schExamInfo.setCount(list.size());
            }
        }

        schExamInfo.calcAvg(total);
        schExamInfo.calcPassRate();

        SchExamInfo schExamInfoForUpdate = (SchExamInfo)SchExamInfo.create(SchExamInfo.class,HANDLER_TYPE.SINGLE_UPDATE);
        schExamInfo.setUrlAnalyse(schExamInfoForUpdate.getUrlAnalyse());

        standardService.update(schExamInfo);

        if(EmptyChecker.notEmpty(updateBatch)){
            SchScoresInfo SchScoresInfo = (SchScoresInfo)RxBaseData.create(SchScoresInfo.class,HANDLER_TYPE.SINGLE_BATCH_UPDATE);
            SchScoresInfo.setDataList(updateBatch);
            SchScoresInfo.getHandlerController().onlyDefault();
            standardService.batchUpdate(SchScoresInfo);
        }

        return Integer.valueOf(0);
    }
}